
/*
 */


package com.core.basic.secure.controller;


import java.text.SimpleDateFormat;
import java.util.Date;

import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.org.rapid_framework.web.scope.Flash;

import com.core.basic.secure.dao.UsersDao;
import com.core.basic.secure.domain.Menu;
import com.core.basic.secure.domain.Users;
import com.core.basic.secure.query.UsersQuery;
import com.core.basic.secure.service.UsersManager;
import common.base.BaseRestSpringController;

/**
 */

@Controller
public class UserOperationController extends BaseRestSpringController<Users,java.lang.Integer>{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private UsersManager usersManager;
	
	private final String LIST_ACTION = "redirect:/menu";
	
	/** 
	 * 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写
	 **/
	public void setUsersManager(UsersManager usersManager) {
		this.usersManager = usersManager;
	}
	/** binder用于bean属性的设置 */
	@InitBinder  
	public void initBinder(WebDataBinder binder) {  
	        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));  
	}
	   

	/**
	 * 增加了@ModelAttribute的方法可以在本controller方法调用前执行,可以存放一些共享变量,如枚举值,或是一些初始化操作
	 */
	@ModelAttribute
	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}
	
	/** 登陆页面 */
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login() {
		return "/login";
	}
	
	/** 主页页面 */
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public String index() {
		return "/index";
	}
	
	/** 根据用户名修改基本信息 */
	@RequestMapping(value="/getUserInfo",method=RequestMethod.GET)
	public String getUserInfo(ModelMap model) {
		String username=SecurityContextHolder.getContext().getAuthentication().getName();
		UsersDao userDao=(UsersDao) usersManager.getEntityDao();
		Users user=userDao.findByProperty("account", username);
		model.addAttribute("users", user);
		return "/users/edit";
	}
	/** 修改基本信息 */
	@RequestMapping(value="/modifyUserInfo",method=RequestMethod.PUT)
	public String modifyUserInfo (ModelMap model,@Valid Users user,BindingResult errors) {
		if(errors.hasErrors()) {
			return "/users/edit";
		}
		
		String username=SecurityContextHolder.getContext().getAuthentication().getName();
		if(username.equals(user.getAccount())){
			UsersDao userDao=(UsersDao) usersManager.getEntityDao();
			Users account=userDao.findByProperty("account", username);
			user.setId(account.getId());
			usersManager.update(user);
			Flash.current().success(UPDATE_SUCCESS);
		}else{
			Flash.current().error("用户名不能修改", username);
		}
		return "/users/edit";
	}
	
}

